(function(ns, win, doc, cfgs){

ns.addPlay(1, {
    Extends: ns.Play,
    options: {
        length: 1,
		allRequired: false
    },
    build: function(ops, self) {
        self.element = new Element('div');
        self.balls = [new ns.Balls({title: '小', min: 1, max: 7, mid: 4}), new ns.Balls({title: '中', min: 8, max: 14, mid: 4}), new ns.Balls({title: '大', min: 15, max: 21, mid: 4})];
        self.balls.invoke('inject', self.element);
    },
    compute: function (balls) {
        var self = this,
            ops = self.options,
            len = balls.length,
            total = eval(balls.invoke('count').join('+')),
            info = {balls: balls.flatten().join(','), nums: total, count: total.combination(ops.length)};
        return info;
    },
    change: function () {
        var self = this,
            balls = self.balls.invoke('get');
            self.info = self.compute(balls);
        self.parent();
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            len,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            len = self.balls.length;
            while (n -- > 0) {
                nums = self.balls[Math.random()*len|0].random(ops.length).join(',');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums}));
            }
            return html.join('');
        } else {
            balls = self.balls.invoke('get');
            info = self.compute(balls);
            return info.count > 0 ? tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: info.balls, balls: info.balls}) : '';            
        }
    }
});

ns.addPlay(2, {
    Extends: ns.getPlay(1)
});

ns.addPlay(4, {
    Extends: ns.getPlay(1),
    options: {
        length: 2
    }
});

ns.addPlay(7, {
    Extends: ns.getPlay(1),
    options: {
        length: 3
    }
});

ns.addPlay(700, {
    Extends: ns.ManulPlay,
    options: {
        length: 3,
        randomType: 7,
        type: 7
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            len,
            balls,
            nums,
            info,
            html = [],
            sort;
        if (n) {
            len = self.balls.length;
            sort = function(a, b){return a - b;}
            while (n -- > 0) {
                nums = self.balls.random(ops.length).sort(sort).join(',');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums}));
            }
        } else {
            balls = self.balls.check();
            if (!balls) {
                App.Alert('您输入的格式不正确，请确认后再试。');
                return false;
            }
            n = balls.length;
            while (n -- > 0) {
                nums = balls[n].sort(sort).join(',');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums}));
            }
        }
        return html.join('');
    }
});

ns.addPlay(10, {
    Extends: ns.getPlay(7)
});

ns.addPlay(1000, {
    Extends: ns.getPlay(700),
    options: {
        randomType: 10,
        type: 10
    },
});

ns.addPlay(12, {
    Extends: ns.getPlay(1),
    options: {
        length: 4
    }
});

ns.addPlay(1200, {
    Extends: ns.getPlay(700),
    options: {
        length: 4,
        randomType: 12,
        type: 12
    },
});

ns.addPlay(15, {
    Extends: ns.getPlay(12)
});

ns.addPlay(1500, {
    Extends: ns.getPlay(1200),
    options: {
        randomType: 15,
        type: 15
    },
});

ns.addPlay(17, {
    Extends: ns.getPlay(1),
    options: {
        length: 5
    }
});

ns.addPlay(1700, {
    Extends: ns.getPlay(700),
    options: {
        length: 5,
        randomType: 17,
        type: 17
    },
});

ns.addPlay(20, {
    Extends: ns.getPlay(17)
});

ns.addPlay(2000, {
    Extends: ns.getPlay(1700),
    options: {
        randomType: 20,
        type: 20
    },
});

ns.addPlay(6, {
    Extends: ns.CombPlay,
    options: {
        sum: 3,
        randomType: 4
    },
    build: function(ops, self) {
        self.element = new Element('div');
        self.balls = [new ns.BileBalls({title: '胆码', min: 1, max: 21, mid: 11, lower:1, upper:ops.sum - 2}), new ns.Balls({title: '拖码', min: 1, max: 21, mid: 11, lower:2, upper:20})];
        self.balls.invoke('inject', self.element);
    },

    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            while (n -- > 0) {
                nums = self.balls[0].random(ops.sum - 1).join(',');//机选时为前N组选
                html.push(tpl({cost: ops.cost, type: ops.randomType/*ops.type*/, name: cfgs.plays[ops.randomType].name/*ops.name*/, count: 1, nums: nums, balls: nums}));
            }
            return html.join('');
        } else {
            if (!self.validate()) {
                App.Alert('胆码加拖码至于要有' + ops.sum + '个');
                return '';
            }
            if (self.balls.invoke('check').every(function(r){return r;})) {
                balls = self.balls.invoke('get');
                info = self.compute(balls);
                return tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: balls.invoke('join', ',').join('|'), balls: info.balls});                          
            }
        }
    }
});

ns.addPlay(9, {
    Extends: ns.getPlay(6),
    options: {
        sum: 4,
        randomType: 7
    }
});

ns.addPlay(14, {
    Extends: ns.getPlay(6),
    options: {
        sum: 5,
        randomType: 12
    }
});

ns.addPlay(19, {
    Extends: ns.getPlay(6),
    options: {
        sum: 6,
        randomType: 17
    }
});

new ns.Lottery(cfgs.lottery);
})(using('HB'), this, document, this.configs || {});
